package com.dq.repository.user;

import com.dq.domain.sys.user.Integral;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

/**
 * @author liuhui
 * @date 2019/9/22 0022 10:19:28
 */
@Repository
public interface IntegralRepository extends JpaRepository<Integral,Long> {
    Page<Integral> findByUserAndDeletedFalseAndCiTypeAndCreateTimeGreaterThanEqualAndCreateTimeLessThanEqualOrderByCreateTimeDesc(SysUser user, Integral.ChangeIntegralType ciType,Long beginTime , Long endTime, Pageable pageable);
    Page<Integral> findByUserAndDeletedFalseAndCreateTimeGreaterThanEqualAndCreateTimeLessThanEqualOrderByCreateTimeDesc(SysUser user,Long beginTime , Long endTime, Pageable pageable);
    Long countByUserAndDeletedFalseAndCiTypeAndCreateTimeGreaterThanEqualAndCreateTimeLessThanEqual(SysUser user, Integral.ChangeIntegralType ciType,Long beginTime , Long endTime);
    @Query("select COALESCE(sum(changeNum),0) from Integral where changeType = ?1 and user = ?2")
    Long sumIntegralAdd(Integral.ChangeType changeType,SysUser user);
    Page<Integral> findByUserAndDeletedFalseOrderByCreateTimeDesc(SysUser user,Pageable pageable);
    Long countByUserAndDeletedFalse(SysUser user);
    @Query("select sum(u.changeNum) from Integral u where u.changeType=:changeType and u.user=:user")
    Long sumTotalIntegralByUser(Integral.ChangeType changeType, WxUser user);

}
